
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from datetime import date, datetime, timedelta
from typing import List, NoReturn
from loguru import logger

from sqlalchemy import column, func, select, update,insert 
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import selectinload, noload
from sqlalchemy.sql import Select


from app.models import Dev


async def get_dev_by_id(db: AsyncSession, id: int) -> Dev:
    dev = await db.execute(select(Dev).where(Dev.id == id))
    return dev.scalars().first()




    # async def get_user_by_id(self, db: AsyncSession, user_id: int) -> User:
    #     return await self.get(db, user_id)

    # async def get_user_roles(self, db: AsyncSession, user_id: int) -> list:
    #     se = await db.execute(
    #         select(self.model)
    #         .where(self.model.id == user_id)
    #         .options(selectinload(self.model.roles))
    #     )
    #     role_ids = [r.id for r in se.scalars().first().roles]
    #     return role_ids

    # async def get_userinfo_by_id(self, db: AsyncSession, user_id: int) -> User:
    #     user = await db.execute(
    #         select(self.model)
    #         .options(selectinload(self.model.department))
    #         .options(noload(self.model.roles))
    #         .where(self.model.id == user_id)
    #     )
    #     return user.scalars().first()

    # async def create_dev(self, db: AsyncSession, obj_in: CreateDev) -> NoReturn:
    #     db_obj = self.model(**obj_in.dict())
    #     # db_obj = self.model(**obj_in   )
    #     # print(obj_in.dict())
    #     db.add(db_obj)
    #     await db.commit()
        # res = await db.execute(insert(self.model).values(obj_in.dict()))


    # async def get_dev_by_name(self, db: AsyncSession, name: str) -> Dev:
    #     db_obj = await db.execute(select(self.model).where(self.model.name == name))
    #     return db_obj.scalars().first()

    # async def get_devid_by_devno(self, db: AsyncSession, dev_no: str) -> Dev:
    #     db_obj = await db.execute(select(self.model).where(self.model.dev_no == dev_no))
    #     res = db_obj.scalars().first()
    #     if not res:
    #         return None
    #     return res.id 

    
    # async def get_dev_by_station(self, db: AsyncSession, name: str) -> Dev:
    #     print(name)
    #     print('通过station获取dev信息')
        # db_obj = await db.execute(select(self.model).where(self.model.name == name))
        # return db_obj.scalars().first()
        # return ''


    # async def get_devs(self, db: AsyncSession ) -> List[Dev]:
    #     devs = await db.execute(select(self.model).order_by(self.model.dev_no))
    #     return devs.scalars().all()

    # async def get_stations_by_name(self, db: AsyncSession, name: str) -> Station:
        # ------------------------------------
        # 1. 在dev表的stations的relationship增加,lazy="joined"
        # dev_obj = await db.execute(select(self.model).where(self.model.name == name))
        # dev = dev_obj.scalars().first()
        # print(dev.stations)
        # 2. 在select后面增加options选项
        # dev_obj = await db.execute(
        #     select(self.model).where(self.model.name == name).options(selectinload(self.model.stations))
        # )
        # dev = dev_obj.scalars().first()
        # # ------------------------------------
        # return dev.stations

    # async def update_dev_state(self, db: AsyncSession,dev_id:int,state:str,color:str,update_time:int):
    #     await db.execute(update(self.model).where(self.model.id==dev_id).values(state=state,state_color=color,update_time=datetime.fromtimestamp(update_time) ))
    #     await db.commit()

    # async def get_user_by_username_with_roles(self, db: AsyncSession, username: str) -> User:
    #     user = await db.execute(
    #         select(self.model)
    #         .options(selectinload(self.model.roles))
    #         .where(self.model.username == username)
    #     )
    #     return user.scalars().first()

    # async def update_user_login_time(self, db: AsyncSession, username: str) -> int:
    #     user = await db.execute(
    #         update(self.model)
    #         .where(self.model.username == username)
    #         .values(last_login=func.now())
    #     )
    #     return user.rowcount

    # async def get_email_by_username(self, db: AsyncSession, username: str) -> str:
    #     user = await self.get_user_by_username(db, username)
    #     return user.email

    # async def get_username_by_email(self, db: AsyncSession, email: str) -> str:
    #     user = await db.execute(select(self.model).where(self.model.email == email))
    #     return user.scalars().first().username

    # async def get_avatar_by_username(self, db: AsyncSession, username: str) -> str:
    #     user = await self.get_user_by_username(db, username)
    #     return user.avatar

    # async def create_user(self, db: AsyncSession, user: CreateUser) -> NoReturn:
    #     user.password = jwt.get_hash_password(user.password)
    #     new_user = self.model(**user.dict(exclude={'role_id'}))
    #     roles_list = []
    #     for i in user.role_id:
    #         roles_list.append(await db.get(Role, i))
    #     new_user.roles = roles_list
    #     db.add(new_user)

    # async def update_userinfo(self, db: AsyncSession, current_user: User, obj: UpdateUser) -> int:
    #     dept = await db.get(Department, obj.department_id)
    #     obj.department_id = dept.id
    #     u = await db.execute(
    #         update(self.model)
    #         .where(self.model.id == current_user.id)
    #         .values(**obj.dict(exclude={'role_id'}))
    #     )
    #     # step1 删除所有角色
    #     for i in list(current_user.roles):
    #         current_user.roles.remove(i)
    #     # step2 添加新的角色
    #     role_list = []
    #     for i in obj.role_id:
    #         role_list.append(await db.get(Role, i))
    #     current_user.roles = role_list
    #     return u.rowcount

    # async def update_avatar(self, db: AsyncSession, current_user: User, avatar: str) -> int:
    #     return await self.update(db, current_user.id, {'avatar': avatar})

    # async def delete_user(self, db: AsyncSession, user_id: int) -> int:
    #     return await self.delete(db, user_id)

    # async def check_email(self, db: AsyncSession, email: str) -> User:
    #     mail = await db.execute(select(self.model).where(self.model.email == email))
    #     return mail.scalars().first()

    # async def delete_avatar(self, db: AsyncSession, user_id: int) -> int:
    #     return await self.update(db, user_id, {'avatar': None})

    # async def reset_password(self, db: AsyncSession, username: str, password: str) -> int:
    #     user = await db.execute(
    #         update(self.model)
    #         .where(self.model.username == username)
    #         .values(password=jwt.get_hash_password(password))
    #     )
    #     return user.rowcount

    # def get_users(self) -> Select:
    #     return select(self.model)\
    #         .order_by(self.model.time_joined.desc())\
    #         .options(selectinload(self.model.department))\
    #         .options(noload(self.model.roles))

    # async def get_user_is_super(self, db: AsyncSession, user_id: int) -> bool:
    #     user = await self.get_user_by_id(db, user_id)
    #     return user.is_superuser

    # async def get_user_is_active(self, db: AsyncSession, user_id: int) -> bool:
    #     user = await self.get_user_by_id(db, user_id)
    #     return user.is_active

    # async def super_set(self, db: AsyncSession, user_id: int) -> int:
    #     super_status = await self.get_user_is_super(db, user_id)
    #     user = await db.execute(
    #         update(User)
    #         .where(User.id == user_id)
    #         .values(is_superuser=False if super_status else True)
    #     )
    #     return user.rowcount


    # async def active_set(self, db: AsyncSession, user_id: int) -> int:
    #     active_status = await self.get_user_is_active(db, user_id)
    #     user = await db.execute(
    #         update(User)
    #         .where(User.id == user_id)
    #         .values(is_active=False if active_status else True)
    #     )
    #     return user.rowcount


# DevDao: CRUDDev = CRUDDev(Dev)
